//work.js
//获取应用实例
const app = getApp();
var url = "http://www.imooc.com/course/ajaxlist";
var page = 0;
var page_size = 5;
var sort = "last";
var is_easy = 0;
var lange_id = 0;
var pos_id = 0;
var unlearn = 0;
// 请求数据
var loadMore = function (that) {
  that.setData({
    hidden: false
  });
  wx.request({
    url: url,
    data: {
      page: page,
      page_size: page_size,
      sort: sort,
      is_easy: is_easy,
      lange_id: lange_id,
      pos_id: pos_id,
      unlearn: unlearn
    },
    success: function (res) {
      console.log(res.data.list);
      //console.info(that.data.list);
      if(res.data.list.length<100){
        that.setData({
          hidden:false
        })
      }
      var list = that.data.list;
      for (var i = page; i < page_size * (page + 1); i++) {
        list.push(res.data.list[i]);
      }
      that.setData({
        list: list
      });
      page++;
      that.setData({
        hidden: true
      });
    },
    fail: function (res) {
      console.log(res);
    }
  });
}
Page({
  data: {
    inputValue: '',
    placeValue: "请输入搜索内容",
    hidden: true,
    list: [],
    scrollTop: 0,
    scrollHeight: 0
  },
  //当键盘输入时触发input事件
  bindKeyInput: function (e) {
    this.setData({
      inputValue: e.detail.value
    })
    //if()
    // https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=1
    // console.log(this.data.inputValue);
  },
  bindFocus: function () {
    this.setData({
      placeValue: ""
    })
    console.log(this.data)
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    //   这里要注意，微信的scroll-view必须要设置高度才能监听滚动事件，所以，需要在页面的onLoad事件中给scroll-view的高度赋值
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        console.log(res);
        that.setData({
          scrollHeight: res.windowHeight
        });
      }
    });
    loadMore(that);
  },
  //页面滑动到底部
  bindDownLoad: function () {
    var that = this;
    loadMore(that);
    console.log("lower");
  },
  scroll: function (event) {
    //该方法绑定了页面滚动时的事件，我这里记录了当前的position.y的值,为了请求数据之后把页面定位到这里来。
    this.setData({
      scrollTop: event.detail.scrollTop
    });
  },
  topLoad: function (event) {
    //   该方法绑定了页面滑动到顶部的事件，然后做上拉刷新
    page = 0;
    this.setData({
      list: [],
      scrollTop: 0
    });
    loadMore(this);
    console.log("lower");
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  //监听用户下拉动作
  onPullDownRefresh: function () {
    console.log('--------下拉刷新-------')
    wx.showNavigationBarLoading() //在标题栏中显示加载

    wx.request({
      url: 'https://URL',
      data: {},
      method: 'GET',
      // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    })
  },
  //页面上拉触底事件的处理函数
  onReachBotto: function () {
    console.log("2");
  },
  onUnload: function () {
    // 页面关闭
  }
})